﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using MySql.Data.MySqlClient;
using Remote.Model;

namespace Remote.Service
{
    // 注意: 使用“重构”菜单上的“重命名”命令，可以同时更改代码、svc 和配置文件中的类名“RemoteArticleService”。
    // 注意: 为了启动 WCF 测试客户端以测试此服务，请在解决方案资源管理器中选择 RemoteArticleService.svc 或 RemoteArticleService.svc.cs，然后开始调试。
    public class RemoteArticleService : IRemoteArticleService
    {

        public ArticleInfo GetArticle(int articleid)
        {
            var strSql = new StringBuilder();
            strSql.Append(" select articleid,lastupdate,articlename,keywords,author,");
            strSql.Append(" a.sortid,intro,lastchapterid,lastchapter,chapters,display,");
            strSql.Append(" b.caption as sortname,coverlink,");
            strSql.Append(" case when fullflag ='1' then '已完本' else '连载' end as writeprogress");
            strSql.Append(" from jieqi_article_article a");
            strSql.Append(" left join jieqi_article_sort b on a.sortid=b.sortid");
            strSql.Append(" where articleid=?articleid");
            MySqlParameter[] parameters =
                {
                    new MySqlParameter("?articleid", MySqlDbType.Int32)
                };
            parameters[0].Value = articleid;
            var model = new ArticleInfo();
            using (MySqlDataReader dr = MySqlHelper.ExecuteReader(MySqlHelper._connectionString,
                                                                  System.Data.CommandType.Text, strSql.ToString(),
                                                                  parameters))
            {
                while (dr.Read())
                {
                    #region

                    if (dr["articleid"] != null && dr["articleid"].ToString() != "")
                    {
                        model.articleid = int.Parse(dr["articleid"].ToString());
                    }
                    if (dr["lastupdate"] != null && dr["lastupdate"].ToString() != "")
                    {
                        model.lastupdate = int.Parse(dr["lastupdate"].ToString());
                    }
                    if (dr["articlename"] != null)
                    {
                        model.articlename = dr["articlename"].ToString();
                    }
                    if (dr["keywords"] != null)
                    {
                        model.keywords = dr["keywords"].ToString();
                    }
                    if (dr["author"] != null)
                    {
                        model.author = dr["author"].ToString();
                    }
                    if (dr["sortid"] != null && dr["sortid"].ToString() != "")
                    {
                        model.sortid = int.Parse(dr["sortid"].ToString());
                    }
                    if (dr["intro"] != null)
                    {
                        model.intro = dr["intro"].ToString();
                    }
                    if (dr["lastchapterid"] != null && dr["lastchapterid"].ToString() != "")
                    {
                        model.lastchapterid = int.Parse(dr["lastchapterid"].ToString());
                    }
                    if (dr["lastchapter"] != null)
                    {
                        model.lastchapter = dr["lastchapter"].ToString();
                    }
                    if (dr["chapters"] != null && dr["chapters"].ToString() != "")
                    {
                        model.chapters = int.Parse(dr["chapters"].ToString());
                    }
                    if (dr["display"] != null && dr["display"].ToString() != "")
                    {
                        model.display = int.Parse(dr["display"].ToString());
                    }
                    if (dr["sortname"] != null)
                    {
                        model.sortname = dr["sortname"].ToString();
                    }
                    model.writeprogress = dr["writeprogress"].ToString();
                    model.coverlink = dr["coverlink"].ToString();
                    #endregion
                }
            }
            return model;
        }

        public IList<ChapterInfo> GetMaxChapterList(int articleid)
        {
            var strSql = new StringBuilder();
            strSql.Append(" select articleid,articlename,lastupdate,chaptername,");
            strSql.Append(" chapterorder,isvip,chaptertype,");
            strSql.Append(" a.chapterid,chapterlink,a.webflag,b.webname ");
            strSql.Append(" from jieqi_article_chapter a ");
            strSql.Append(" join sysweb b on a.webflag=b.webflag");
            strSql.Append(
                " join (select max(chapterid) chapterid from jieqi_article_chapter where  articleid=?articleid group by webflag) c");
            strSql.Append(" where a.articleid=?articleid and a.chapterid=c.chapterid");
            MySqlParameter[] parameters = {new MySqlParameter("?articleid", MySqlDbType.Int32)};
            parameters[0].Value = articleid;
            IList<ChapterInfo> list = new List<ChapterInfo>();
            using (
                MySqlDataReader dr = MySqlHelper.ExecuteReader(MySqlHelper._connectionString,
                                                               System.Data.CommandType.Text, strSql.ToString(),
                                                               parameters))
            {
                while (dr.Read())
                {
                    var model = new ChapterInfo();

                    #region

                    if (dr["chapterid"] != null && dr["chapterid"].ToString() != "")
                    {
                        model.chapterid = int.Parse(dr["chapterid"].ToString());
                    }
                    if (dr["articleid"] != null && dr["articleid"].ToString() != "")
                    {
                        model.articleid = int.Parse(dr["articleid"].ToString());
                    }
                    if (dr["articlename"] != null)
                    {
                        model.articlename = dr["articlename"].ToString();
                    }
                    if (dr["lastupdate"] != null && dr["lastupdate"].ToString() != "")
                    {
                        model.lastupdate = int.Parse(dr["lastupdate"].ToString());
                    }
                    if (dr["chaptername"] != null)
                    {
                        model.chaptername = dr["chaptername"].ToString();
                    }
                    if (dr["isvip"] != null && dr["isvip"].ToString() != "")
                    {
                        model.isvip = int.Parse(dr["isvip"].ToString());
                    }
                    if (dr["chaptertype"] != null && dr["chaptertype"].ToString() != "")
                    {
                        model.chaptertype = int.Parse(dr["chaptertype"].ToString());
                    }

                    #endregion
                    if(dr["isvip"].ToString()=="1")
                    {
                        model.flag = "VIP";
                    }
                    else if (dr["chaptertype"].ToString().Equals("1"))
                    {
                        model.flag = "图";
                    }
                    else
                    {
                        model.flag = "New";
                    }
                    model.chapterlink = dr["chapterlink"].ToString();
                    model.webflag = dr["webflag"].ToString();
                    model.webname = dr["webname"].ToString();
                    list.Add(model);
                }
            }
            return list;
        }

        public IList<ChapterInfo> GetBookReader(int articleid, string webflag)
        {
            var strSql = new StringBuilder();
            strSql.Append(" select articleid,articlename,lastupdate,chaptername,");
            strSql.Append(" chapterorder,isvip,chaptertype,");
            strSql.Append(" a.chapterid,chapterlink,a.webflag,b.webname ");
            strSql.Append(" from jieqi_article_chapter a ");
            strSql.Append(" join sysweb b on a.webflag=b.webflag");
            strSql.Append(" where chaptertype=0 and a.articleid=?articleid and a.webflag=?webflag");
            strSql.Append(" order by chapterorder");
            MySqlParameter[] parameters = { new MySqlParameter("?articleid", MySqlDbType.Int32) ,
                                           new MySqlParameter("?webflag", MySqlDbType.String) };
            parameters[0].Value = articleid;
            parameters[1].Value = webflag;
            IList<ChapterInfo> list = new List<ChapterInfo>();
            using (MySqlDataReader dr = MySqlHelper.ExecuteReader(MySqlHelper._connectionString,
                                                               System.Data.CommandType.Text, strSql.ToString(),
                                                               parameters))
            {
                while (dr.Read())
                {
                    var model = new ChapterInfo();

                    #region

                    if (dr["chapterid"] != null && dr["chapterid"].ToString() != "")
                    {
                        model.chapterid = int.Parse(dr["chapterid"].ToString());
                    }
                    if (dr["articleid"] != null && dr["articleid"].ToString() != "")
                    {
                        model.articleid = int.Parse(dr["articleid"].ToString());
                    }
                    if (dr["articlename"] != null)
                    {
                        model.articlename = dr["articlename"].ToString();
                    }
                    if (dr["lastupdate"] != null && dr["lastupdate"].ToString() != "")
                    {
                        model.lastupdate = int.Parse(dr["lastupdate"].ToString());
                    }
                    if (dr["chaptername"] != null)
                    {
                        model.chaptername = dr["chaptername"].ToString();
                    }
                    if (dr["isvip"] != null && dr["isvip"].ToString() != "")
                    {
                        model.isvip = int.Parse(dr["isvip"].ToString());
                    }
                    if (dr["chaptertype"] != null && dr["chaptertype"].ToString() != "")
                    {
                        model.chaptertype = int.Parse(dr["chaptertype"].ToString());
                    }

                    #endregion
                    if (dr["isvip"].ToString() == "1")
                    {
                        model.flag = "VIP";
                    }
                    else if (dr["chaptertype"].ToString().Equals("1"))
                    {
                        model.flag = "图";
                    }
                    //else
                    //{
                    //    model.flag = "New";
                    //}
                    model.chapterlink = dr["chapterlink"].ToString();
                    model.webflag = dr["webflag"].ToString();
                    //model.webname = dr["webname"].ToString();
                    list.Add(model);
                }
            }
            return list;
        }
    }
}
